<template>
	<view class="content">
		<view class="top" v-if="!userInfo.memberLevelId">
			<image src="/static/vb.jpg" mode="" class="bg"></image>
			<view class="con">
				<view class="user_info">
					<view class="image">
						<image :src="userInfo.icon || '/static/avatar.png'" mode=""></image>
					</view>
					<view class="text">
						<view class="t1">
							{{userInfo.nickname}}
						</view>
						<view class="t2">
							尚未开通会员
						</view>
					</view>
				</view>
				<view class="dec">
					开通会员·享品牌日5折特惠及多种特权
				</view>
			</view>
		</view>
		<view class="top vip" v-else>
			<image src="/static/vbs.jpg" mode="" class="bg"></image>
			<view class="con">
				<view class="user_info">
					<view class="image">
						<image :src="userInfo.icon || '/static/avatar.png'" mode=""></image>
					</view>
					<view class="text">
						<view class="t1">
							{{userInfo.nickname}}
						</view>
						<view class="sss">
							<view class="sv">
								{{userInfo.memberLevelName}}
							</view>
							<image v-if="userInfo.memberIcon" :src="userInfo.memberIcon" mode="heightFix"></image>
						</view>
						<view class="time">
							会员码：{{userInfo.memberCode}}
						</view>
						
						<view class="vipCode" v-if="userInfo.memberLevelId" @click="show = true">
							<image src="/static/code.png" mode=""></image>
							<u-icon name="arrow-right" color="#FFEAC8" size="16"></u-icon>
						</view>
					</view>
				</view>
			</view>
		</view>
		<view class="conBox">
			<swiper class="swiper" circular :indicator-dots="true"   @change="handleChange" :style="{ height: swiperHeight + 'px' }">
				<swiper-item v-for="(item,index) in list" :key="item.id">
					<view class="" :id="`item${index}`"  :data-index="index">
						
					
					<view class="title">
						{{item.des}}
					</view>
					<view class="sbox">
						<view class="item" v-for="sitem in item.rights" :key="sitem.title">
							<image :src="sitem.icon" mode=""></image>
							<text>{{sitem.title}}</text>
						</view>
					</view>
					<view class="sbtn"  v-if="isSubmit(item.id)">
						已开通
					</view>
					<view class="sbtn" @click="handleSubmit(item.id)" v-else-if="item.online">
						{{item.amount}}元 立即开通
					</view>
					<button open-type="contact" class="sbtn" v-else-if="item.online == 0">
						联系客服
					</button>
					</view>
				</swiper-item>
			</swiper>
		</view>
		<view class="bot">
			<view class="title">
				会员权利说明
			</view>
			<view class="con">
				<u-parse :content="text" :tagStyle="style"></u-parse>
			</view>
		</view>
		
		<u-overlay :show="show">
			<view class="vipCodeBox">
				<view class="sbox">
					<image src="/static/col.png" class="col" mode="" @click="show = false"></image>
					<image :src="userInfo.shardQrCode" class="code" mode=""></image>
				</view>
			</view>
		</u-overlay>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				show:false,
				text: '', 
				style: { 
					p: 'color: #545454;font-size:32rpx;line-height:44rpx;margin-bottom:20rpx',
					span: 'font-size: 30rpx',
					img:'width:100% !important;height:auto !important;display:block'
				},
				list: [],
				 current: 0,
				swiperHeight:'',
				 itemsHeight: []
			}
		},
		onLoad() {
			this.getList()
			this.getText()
		},

		computed: {
			userInfo() {
				return this.$store.state.userInfo
			}
		},
		watch: {
		    list: {
		      handler() {
		        // 数据变化时重新计算高度
		        this.$nextTick(() => {
		          setTimeout(() => {
		            this.getAllItemsHeight()
		          }, 100)
		        })
		      },
		      deep: true
		    }
		  },
		methods: {
			
			handleChange(e) {
			      this.current = e.detail.current
			      // 切换时更新高度
			      this.updateSwiperHeight(e.detail.current)
			    },
			    // 更新swiper高度
			    updateSwiperHeight(index) {
			      if (this.itemsHeight[index]) {
			        this.swiperHeight = this.itemsHeight[index]
			      }
			    },
			    // 获取所有item的高度
			    async getAllItemsHeight() {
			      for (let i = 0; i < this.list.length; i++) {
			        try {
			          const query = uni.createSelectorQuery().in(this)
			          query.select(`#item${i}`).boundingClientRect()
			          const rect = await new Promise((resolve, reject) => {
			            query.exec(ret => {
			              if (ret && ret[0]) {
			                resolve(ret[0])
			              } else {
			                reject('获取高度失败')
			              }
			            })
			          })
			          this.itemsHeight[i] = rect.height+30
			          // 如果是当前显示的item，立即更新swiper高度
			          if (i === this.current) {
			            this.swiperHeight = rect.height+30
			          }
			        } catch (error) {
			          console.error('1获取高度失败:', error)
			        }
			      }
			    },
				
				
			isSubmit(id){
				// 将字符串 a 拆分成数组
				if(this.userInfo.memberLevelId){
					
					const idArray = this.userInfo.memberLevelId.split(',').map(Number);
					// 检查 targetId 是否存在于数组中
					return idArray.includes(id);
				}else{
					return false
				}
			},
			getText() {
				this.$http({
					url: "/umsContent/queryById",
					method: "get",
					data: {
						id: 6
					}
				}).then(data => {
					this.text = data.content
				})
			},
			getList() {
				this.$http({
					url: "/umsVip/paginQuery",
					method: "get",
					data: {
						pageSize: 5,
						pageNum: 1
					}
				}).then(data => {
					this.list = data.list
					setTimeout(() => {
					  this.getAllItemsHeight()
					}, 100)
				})
			},
			handleSubmit(id) {
				this.$http({
					url: "/umsVip/generateOrder",
					method: "get",
					data: {
						id
					}
				}).then(data => {
					uni.requestPayment({
						timeStamp: data.timeStamp,
						nonceStr: data.nonceStr,
						package: data.package,
						signType: data.signType,
						paySign: data.paySign,
						success: (res) => {
							uni.showToast({
								title: '支付成功'
							})
							 this.$store.dispatch('getUserInfo'); 
						},
						fail: (res) => {
							if (res.errMsg == 'requestPayment:fail cancel') {
								uni.showToast({
									title: '取消支付',
									icon: 'none'
								})
							}
						}
					})
				})
			}
		}
	}
</script>

<style>
	page {
		background: #f8f8f8;
	}
</style>

<style lang="scss" scoped>
	.swiper {
		height: 650rpx;
	}
.vipCodeBox{
		height: 100vh;
		width: 100%;
		display: flex;
		align-items: center;
		justify-content: center;
		.sbox{
			width: 540rpx;
			height: 540rpx;
			background: #fff;
			border-radius: 20rpx;
			display: flex;
			align-items: center;
			justify-content: center;
			position: relative;
			.col{
				display: block;
				position: absolute;
				right: -32rpx;
				width: 64rpx;
				height: 64rpx;
				top: -32rpx;
			}
			.code{
				width: 400rpx;
				display: block;
				height: 400rpx;
			}
		}
	}
	.content {
		padding: 24rpx;

		.bot {
			background: #fff;
			margin-top: 30rpx;
			padding: 24rpx;

			.title {
				font-size: 30rpx;
				font-weight: bold;
				color: #020202;
				margin-bottom: 30rpx;
			}

			.con {
				font-size: 24rpx;
				color: #666666;
			}
		}

		.conBox {
			background: #fff;
			margin-top: 30rpx;
			padding: 30rpx;
			box-sizing: border-box;

			.title {
				font-size: 32rpx;
				text-align: center;
				margin-bottom: 38rpx;
				font-weight: bold;
			}

			.sbtn {
				width: 590rpx;
				height: 72rpx;
				background: linear-gradient(90deg, #363330 0%, #12100E 100%);
				border-radius: 49rpx;
				text-align: center;
				line-height: 72rpx;
				margin: 0 auto;
				font-size: 28rpx;
				color: #fff;
			}

			.sbox {
				display: flex;
				flex-wrap: wrap;
				justify-content: space-around;

				.item {
					width: 30%;
					margin-bottom: 40rpx;

					image {
						width: 68rpx;
						height: 68rpx;
						display: block;
						margin: 0 auto;
						margin-bottom: 12rpx;
					}

					text {
						display: block;
						text-align: center;
						font-size: 24rpx;
						color: #333;
					}
				}
			}
		}

		.top {
			width: 100%;
			position: relative;

			.bg {
				position: absolute;
				left: 0;
				top: 0;
				width: 100%;
				height: 288rpx;
			}

			.dec {
				font-size: 28rpx;
				color: #FFEAC8;
				font-weight: bold;
				margin-top: 40rpx;
			}

			.user_info {
				display: flex;
				align-items: center;

				.text {
					margin-left: 30rpx;

					.t1 {
						font-size: 32rpx;
						color: #fff;
						font-weight: bold;
					}

					.t2 {
						font-size: 24rpx;
						color: rgba(255, 255, 255, .6);
						margin-top: 14rpx;
					}
				}

				.image {
					width: 120rpx;
					height: 120rpx;
					border-radius: 50%;
					border: 4rpx solid #FFFFFF;
					overflow: hidden;

					image {
						width: 120rpx;
						height: 120rpx;
					}
				}
			}

			.con {
				position: relative;
				z-index: 9;
				padding: 40rpx 30rpx;
				box-sizing: border-box;
			}

			&.vip {
				background: linear-gradient(90deg, #363330 0%, #12100E 100%);
				height: auto;
				border-radius: 4rpx;
				padding: 40rpx 30rpx;
				height: 240rpx;
				box-sizing: border-box;
				box-sizing: border-box;

				.time {
					font-size: 24rpx;
					color: #FFEAC8;
					margin-top: 20rpx;
				}
				
				.sss{
					 display: flex;
					 align-items: center;
					 margin-top: 8rpx;
					 image{
						 display: block;
						 height: 40rpx;
						 margin-left: 10rpx;
					 }
				}
				.sv {
					background: linear-gradient(90deg, #e9d2ad 0%, #d1a664 100%);
					display: inline-block;
					padding: 4rpx 8rpx;
					color: #884613;
					font-weight: bold;
					border-radius: 5rpx;
					font-size: 20rpx;
					position: relative;
				}

				.con {
					padding: 0;
				}

				.text {
					position: relative;
					flex: 1;
					.vipCode{
						position: absolute;
						right: 0rpx;
						bottom: 0rpx;
						display: flex;
						align-items: center;
						image{
							display: block;
							width: 40rpx;
							height: 40rpx;
							margin-right: 6rpx;
						}
					}
					.t1 {
						color: #FFEAC8;
						font-size: 34rpx;
					}
				}
				.bg {
					height: 240rpx;
				}
				.image {
					border: 2rpx solid #F7D5AC; 
					width: 140rpx;
					height: 140rpx;
					border-radius: 50%;
					overflow: hidden;
					image{
						 width: 140rpx;
						 height: 140rpx;
					}
				}
			}
		}
	}
</style>